package me.firstapp.repository.weixin.activity.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.module.weixin.activity.RedPackRecord;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.weixin.activity.RedPackRecordRepository;

@Repository
public class RedPackRecordRepositoryImpl extends EntityRepositoryImpl<RedPackRecord>
		implements RedPackRecordRepository {

	@Override
	protected Class<RedPackRecord> getPersistentClass() {
		return RedPackRecord.class;
	}

	@Override
	public RedPackRecord findByProductIdAndOpenId(Long productId, String openId) {
		StringBuilder sql = new StringBuilder(" FROM RedPackRecord bean WHERE 1=1 ");
		Map<String, Object> parameters = new HashMap<>();

		sql.append(" AND bean.productId = :productId ");
		parameters.put("productId", productId);

		sql.append(" AND bean.openId = :openId ");
		parameters.put("openId", openId);

		List<RedPackRecord> result = super.query(RedPackRecord.class, sql.toString(), parameters);
		return result.isEmpty() ? null : result.get(0);
	}

}
